//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<mspace.h>
///	@path	~/src/lib/memory/
///	@date	2007/11/18
///	@desc	'mspace' memory manager.

#pragma once

#include "xeres.h"

#include <set>

namespace xeres {

	/*!
		\class	mspace
		\brief	Customized memory manager.
	*/
	class mspace
	{
	public:

		/// \ctor
		mspace( size_t capacity = 0 );

		/// \dtor
		~mspace( void );

	//@ methods

		/*!
			\brief		Allocate space.
		*/
		void * malloc( size_t size );

		/*!
			\brief		Allocate aligned space.
		*/
		void * malloc_aligned( size_t size );

		/*!
			\brief		Re-allocate space.
		*/
		void * realloc( void * space , size_t size );

		/*!
			\brief		Free allocated space.
		*/
		void free( void * space );

	private:

		/*!
			\brief		Node walker function.
		*/
		static inline bool internal_walker( void * ptr , size_t blocksize , void * data );

		/*!
			\brief		Walker function to release nodes.
		*/
		static inline bool release_walk( void * ptr , struct Field * field , size_t size , void * data );

		// walk though space nodes
		inline void walkthough( void );

	//@ data

		// mspace
		typedef void * _mspace;
		_mspace m_mspace;

		std::set< void * >	m_largeSpace;

	};


} // namespace xeres
